jQuery.extend({
    // 统一的 AJAX 请求配置
    _request: function (action, data, callback) {
        $.ajax({
            method: "POST",
            url: "/_login.html",
            dataType: "json",
            data: { action, ...data },
            success: callback,
            error: function (xhr, status, error) {
                console.log(xhr.responseText);
                callback({ error: 99, info: error });
            }
        });
    },
    // 获取url参数
    urlRequest: function (param) {
        if (!param) return null;

        const url = new URL(location.href.replace(/#$/, ''));
        return url.searchParams.get(param.toLowerCase());
    },
    // 和后端相同格式规则的hash函数
    hashBase64: function (str, protocol = "SHA256") {
        result = str
        switch (protocol) {
            case "MD5":
                result = String.fromCharCode(...md5.array(str))
                break;
            case "SHA1":
                result = String.fromCharCode(...sha1.array(str))
                break;
            case "SHA224":
                result = String.fromCharCode(...sha224.array(str))
                break;
            case "SHA256":
                result = String.fromCharCode(...sha256.array(str))
                break;
        }
        result = btoa(result).replaceAll("\/", "_").replaceAll("+", "-");
        return result;
    },
    // 登录
    login: function (opts, callback) {
        const options = {
            loginName: "",
            passwd: "",
            captcha: "",
            ...opts
        };

        const handlePreLogin = (res) => {
            if (res.error !== 0) {
                return callback(res);
            }
            let hashedPasswd = options.passwd;
            if (res.serverSalt) {
                hashedPasswd = this.hashBase64(options.passwd + res.serverSalt, "SHA256");
            }
            if (res.sessionSalt) {
                hashedPasswd = this.hashBase64(hashedPasswd + res.sessionSalt, "SHA256");
            }
            this._request("login", { passwd: hashedPasswd }, callback);
        };

        if (!options.mask) {
            this._request("prelogin", {
                loginName: options.loginName,
                captcha: options.captcha
            }, handlePreLogin);
            return;
        }

        // 先验证掩码，成功后继续登录流程
        this._request("checkcode", {
            type: "1",
            mask: options.mask
        }, (res) => {
            if (res.error === 0) {
                this._request("prelogin", {
                    loginName: options.loginName,
                    captcha: options.captcha
                }, handlePreLogin);
            } else {
                callback(res);
            }
        });
    },
    // 登出
    logout: function (callback) {
        this._request("logout", {}, callback);
    },
    // 登录状态
    loginState: function (callback) {
        this._request("loginstate", {}, callback);
    },
    // 修改密码
    upppw: function (opts, callback) {
        const options = {
            passwd: "",
            ...opts
        };

        this._request("upppw", { passwd: options.passwd }, callback);
    }
});